package com.taobao.shopcart.service.impl;

import com.taobao.base.model.RestResponse;
import com.taobao.shopcart.mapper.OrdersMapper;
import com.taobao.shopcart.model.dto.OrderDto;
import com.taobao.shopcart.model.po.Orders;
import com.taobao.shopcart.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrdersMapper ordersMapper;



    // 获取未支付订单
    @Override
    public RestResponse<List<OrderDto>> getUnpaidOrder(int userId) {

        List<Orders> orders = ordersMapper.selectOrderStatusByUserId(userId);
        ArrayList<OrderDto> orderDtos = new ArrayList<>();
        // 转成OrderDto
        for (Orders order : orders) {
            OrderDto orderDto = new OrderDto();
            BeanUtils.copyProperties(orderDto, order);
            orderDtos.add(orderDto);

        }

        return RestResponse.success(orderDtos);
    }

    // 获取待发货订单
    @Override
    public RestResponse<List<OrderDto>> getUnshippedOrder(int userId) {

        List<Orders> orders = ordersMapper.selectOrderStatusByShipped(userId);
        ArrayList<OrderDto> orderDtos = new ArrayList<>();
        // 转成OrderDto
        for (Orders order : orders) {
            OrderDto orderDto = new OrderDto();
            BeanUtils.copyProperties(orderDto, order);
            orderDtos.add(orderDto);

        }

        return RestResponse.success(orderDtos);
    }

    @Override
    public RestResponse<List<Orders>> getAllOrder(int userId) {

        List<Orders> orders = ordersMapper.selectAllByUserId(userId);
        // todo:清洗数据



        return RestResponse.success(orders);

    }
}
